package com.rederxu.tools;

import java.io.File;

import com.rederxu.cmd.CMD;
import com.rederxu.cmd.onCMDListener;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;

public class ScreenShotUtils {

	/**
	 * 无root权限的时候只能这样截屏
	 * <h1>有root权限时不建议使用本方法（本方法在部分机型有权限时会截屏为黑色）<h1>
	 * */
	public static boolean shot(Activity activity,String imagePath) {
		// View是你需要截图的View
		View view = activity.getWindow().getDecorView();
	    Bitmap bmp = Bitmap.createBitmap(DisplayUtils.getScreenWidth(activity), DisplayUtils.getScreenHeight(activity), Config.ARGB_8888);
	    view.draw(new Canvas(bmp));
		if(bmp == null){
			Log.e("shot", "bitmap is null");
			return false;
		}
		return BitmapUtil.saveBitmap(bmp, new File(imagePath));
	}
	
	public static void shot(final String imagePath,final onScreenshotListener l){
		String cmd = "screencap -p " + imagePath;
		CMD.execRootCmd(cmd, new onCMDListener() {
			
			@Override
			public void onCMD(String result) {
				Log.e("shot", result);
				if(result.contains("successful")){
					l.onScreenshot(true,imagePath);
				}else{
					l.onScreenshot(false,imagePath);
				}
			}
		});
	}
}
